import 'package:agora_rtc_engine/rtc_engine.dart';
import 'package:star_author/utils/common_util/num_util.dart';
import 'package:star_author/utils/rouces_util.dart';
import 'package:star_author/widgets/select_circle_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../utils/utils.dart';

const String lighteningLevel = "MB"; //美白

const String smoothnessLevel = "MP"; //磨皮

const String rednessLevel = "HR"; //红润

const String contrast = "DB"; //对比

const String darkLightLevel = "AG"; //暗光增强

const String ColorEnhancement = "SC"; //色彩增强

List beauty = [
  {
    "title": "美白",
    "type": "slider",
    "value": 0.0,
    "suffix": lighteningLevel,
  },
  {
    "title": "磨皮",
    "type": "slider",
    "value": 0.0,
    "suffix": smoothnessLevel,
  },
  {
    "title": "红润",
    "type": "slider",
    "value": 0.0,
    "suffix": rednessLevel,
  },
  // {
  //   "title": "锐化",
  //   "type": "slider",
  //   "value": 0.0,
  //   "suffix": sharpnessLevel,
  // },
  {
    "title": "对比度",
    "type": "button",
    "value": 1,
    "suffix": contrast,
    "buttonList": [
      {
        "title": "低",
        "value": 0,
        "data": LighteningContrastLevel.Low,
      },
      {
        "title": "中",
        "value": 1,
        "data": LighteningContrastLevel.Normal,
      },
      {
        "title": "高",
        "value": 2,
        "data": LighteningContrastLevel.High,
      },
    ],
  },
  {
    "title": "色彩增强",
    "type": "sliderAndButton",
    "suffix": ColorEnhancement,
    "button_value": 1,
    "strength_value": 50.0,
    "skin_protect_value": 100.0,
    "buttonList": [
      {
        "title": "开启",
        "value": true,
      },
      {
        "title": "关闭",
        "value": false,
      },
    ],
  },
  {
    "title": "暗光增强",
    "type": "button",
    "value": 1,
    "suffix": darkLightLevel,
    "buttonList": [
      {
        "title": "开启",
        "value": true,
      },
      {
        "title": "关闭",
        "value": false,
      },
    ]
  },
];

class BeautyDialog extends StatefulWidget {
  RtcEngine engine;
  BeautyDialog({Key? key, required this.engine}) : super(key: key);

  @override
  State<BeautyDialog> createState() => _BeautyDialogState();
}

class _BeautyDialogState extends State<BeautyDialog> {
  // static const String sharpnessLevel = "RH"; //锐化

  int currentIndex = 0;

  double max = 100;

  int divisions = 100; //分为几块，比如设置为10，Slider只能滑动到10个位置

  @override
  Widget build(BuildContext context) {
    return Container(
      height:
          beauty[currentIndex]['suffix'] == ColorEnhancement ? 300.w : 178.w,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(16.w),
          topRight: Radius.circular(16.w),
        ),
      ),
      width: Get.width,
      padding: EdgeInsets.only(
        left: 12.w,
        top: 12.w,
        right: 12.w,
      ),
      child: Column(
        children: [
          GestureDetector(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                RoucesUtil.getAssetsIcon("ic_live_beauty_reset_lt", 12),
                SizedBox(
                  width: 2.w,
                ),
                Text(
                  "重置",
                  style: TextStyle(
                    fontSize: 12,
                    color: rgba(51, 51, 51, 1),
                  ),
                ),
              ],
            ),
            onTap: () {
              reset();
            },
          ),
          SizedBox(
            height: 20.w,
          ),
          buildContent(),
          SizedBox(
            height: 20.w,
          ),
          Container(
            child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(
                children: List.generate(
                  beauty.length,
                  (index) => GestureDetector(
                    child: Container(
                      margin: EdgeInsets.only(
                        right: 20.w,
                      ),
                      child: Text(
                        beauty[index]['title'],
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: rgba(51, 51, 51, 1),
                          fontWeight: currentIndex == index
                              ? FontWeight.bold
                              : FontWeight.w400,
                        ),
                      ),
                    ),
                    onTap: () {
                      setState(() {
                        currentIndex = index;
                      });
                    },
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  getValue(suffix, {String value: "value"}) {
    int index = beauty.indexWhere((element) => element['suffix'] == suffix);
    return NumUtil.divide(beauty[index][value], 100);
  }

  sliderOnchangeEnd() {
    double lightening = getValue(lighteningLevel);
    double smoothness = getValue(smoothnessLevel);
    double redness = getValue(rednessLevel);
    //当前sdk没有这个美颜参数
    // double sharpness = getValue(sharpnessLevel);
    setBeautyEffectOptions(
      true,
      lighteningLevel: lightening,
      smoothnessLevel: smoothness,
      rednessLevel: redness,
    );
  }

  buildContent() {
    switch (beauty[currentIndex]['type']) {
      case "slider":
        return Column(
          children: [
            Text(
              "${beauty[currentIndex]['value'].toInt()}",
              style: TextStyle(
                fontSize: 12,
                color: rgba(51, 51, 51, 1),
              ),
            ),
            SizedBox(
              height: 5.w,
            ),
            Container(
              child: SliderTheme(
                data: SliderTheme.of(context).copyWith(
                  //已拖动的颜色
                  activeTrackColor: rgba(240, 69, 41, 1),
                  //未拖动的颜色
                  inactiveTrackColor: Color(0x36D8D8D8),
                  //提示进度的气泡的背景色
                  valueIndicatorColor: Color(0xCCD8D8D8),
                  //提示进度的气泡文本的颜色
                  valueIndicatorTextStyle: TextStyle(
                    color: Colors.white,
                  ),
                  trackHeight: 4.w,
                  tickMarkShape: RoundSliderTickMarkShape(
                    //继承SliderTickMarkShape可自定义刻度形状
                    tickMarkRadius: 0, //刻度大小
                  ),
                  thumbShape: RoundSliderThumbShape(
                    //可继承SliderComponentShape自定义形状
                    disabledThumbRadius: 0, //禁用时滑块大小
                    enabledThumbRadius: 8.w, //滑块大小
                  ),
                  overlayShape: RoundSliderOverlayShape(
                    //可继承SliderComponentShape自定义形状
                    overlayRadius: 0, //滑块外圈大小
                  ),
                  //滑块中心的颜色
                  thumbColor: Colors.white,
                ),
                child: Slider(
                  value: beauty[currentIndex]['value'],
                  max: max,
                  divisions: divisions,
                  onChanged: (double value) {
                    if (mounted) {
                      setState(() {
                        beauty[currentIndex]['value'] = value;
                      });
                    }
                  },
                  onChangeEnd: (val) async {
                    setState(() {
                      beauty[currentIndex]['value'] = val;
                      sliderOnchangeEnd();
                    });
                  },
                  onChangeStart: (val) {},
                ),
              ),
              padding: EdgeInsets.only(
                left: 20.w,
              ),
            ),
          ],
        );
      case "button":
        return Container(
          width: Get.width,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: List.generate(
              (beauty[currentIndex]['buttonList'] ?? []).length,
              (index) {
                onTap() {
                  setState(() {
                    beauty[currentIndex]['value'] = index;
                  });
                  if (beauty[currentIndex]['suffix'] == darkLightLevel) {
                    setLowlightEnhanceOptions(
                        beauty[currentIndex]['buttonList'][index]['value']);
                  } else if (beauty[currentIndex]['suffix'] ==
                      ColorEnhancement) {
                  } else {
                    double lightening = getValue(lighteningLevel);
                    double smoothness = getValue(smoothnessLevel);
                    double redness = getValue(rednessLevel);
                    setBeautyEffectOptions(
                      true,
                      lighteningContrastLevel: beauty[currentIndex]
                          ['buttonList'][index]['data'],
                      lighteningLevel: lightening,
                      smoothnessLevel: smoothness,
                      rednessLevel: redness,
                    );
                  }
                }

                return GestureDetector(
                  child: Container(
                    child: Row(
                      children: [
                        SelectCircleWidget(
                          callBack: (type) {
                            onTap();
                          },
                          isSelect: beauty[currentIndex]['value'] == index,
                          selectColor: RGBA(240, 69, 41, 1),
                          size: 18,
                        ),
                        SizedBox(
                          width: 3.w,
                        ),
                        Text(
                          "${beauty[currentIndex]['buttonList'][index]['title']}",
                          style: TextStyle(
                            fontSize: 12.sp,
                            color: rgba(51, 51, 51, 1),
                          ),
                        )
                      ],
                    ),
                  ),
                  onTap: onTap,
                );
              },
            ),
          ),
        );
      case "sliderAndButton":
        onSliderEnd() {
          double strength = getValue(ColorEnhancement, value: "strength_value");
          double skin_protect_value =
              getValue(ColorEnhancement, value: "skin_protect_value");
          setColorEnhanceOptions(
            true,
            skinProtectLevel: skin_protect_value,
            strengthLevel: strength,
          );
        }
        return Container(
          child: Column(
            children: [
              Container(
                width: Get.width,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: List.generate(
                    (beauty[currentIndex]['buttonList'] ?? []).length,
                    (index) {
                      onTap() {
                        setState(() {
                          beauty[currentIndex]['button_value'] = index;
                        });
                        double strength =
                            getValue(ColorEnhancement, value: "strength_value");
                        double skin_protect_value = getValue(ColorEnhancement,
                            value: "skin_protect_value");
                        setColorEnhanceOptions(
                          beauty[currentIndex]['buttonList'][index]['value'],
                          strengthLevel: strength,
                          skinProtectLevel: skin_protect_value,
                        );
                      }

                      return GestureDetector(
                        child: Container(
                          child: Row(
                            children: [
                              SelectCircleWidget(
                                callBack: (type) {
                                  onTap();
                                },
                                isSelect: beauty[currentIndex]
                                        ['button_value'] ==
                                    index,
                                selectColor: RGBA(240, 69, 41, 1),
                                size: 18,
                              ),
                              SizedBox(
                                width: 3.w,
                              ),
                              Text(
                                "${beauty[currentIndex]['buttonList'][index]['title']}",
                                style: TextStyle(
                                  fontSize: 12.sp,
                                  color: rgba(51, 51, 51, 1),
                                ),
                              )
                            ],
                          ),
                        ),
                        onTap: onTap,
                      );
                    },
                  ),
                ),
              ),
              SizedBox(
                height: 10.w,
              ),
              Container(
                child: Column(
                  children: [
                    Text(
                      "${beauty[currentIndex]['strength_value'].toInt()}",
                      style: TextStyle(
                        fontSize: 12,
                        color: rgba(51, 51, 51, 1),
                      ),
                    ),
                    SizedBox(
                      height: 5.w,
                    ),
                    Container(
                      child: SliderTheme(
                        data: SliderTheme.of(context).copyWith(
                          //已拖动的颜色
                          activeTrackColor: rgba(240, 69, 41, 1),
                          //未拖动的颜色
                          inactiveTrackColor: Color(0x36D8D8D8),
                          //提示进度的气泡的背景色
                          valueIndicatorColor: Color(0xCCD8D8D8),
                          //提示进度的气泡文本的颜色
                          valueIndicatorTextStyle: TextStyle(
                            color: Colors.white,
                          ),
                          trackHeight: 4.w,
                          tickMarkShape: RoundSliderTickMarkShape(
                            //继承SliderTickMarkShape可自定义刻度形状
                            tickMarkRadius: 0, //刻度大小
                          ),
                          thumbShape: RoundSliderThumbShape(
                            //可继承SliderComponentShape自定义形状
                            disabledThumbRadius: 0, //禁用时滑块大小
                            enabledThumbRadius: 8.w, //滑块大小
                          ),
                          overlayShape: RoundSliderOverlayShape(
                            //可继承SliderComponentShape自定义形状
                            overlayRadius: 0, //滑块外圈大小
                          ),
                          //滑块中心的颜色
                          thumbColor: Colors.white,
                        ),
                        child: Slider(
                          value: beauty[currentIndex]['strength_value'],
                          max: max,
                          divisions: divisions,
                          onChanged: (double value) {
                            if (mounted) {
                              setState(() {
                                beauty[currentIndex]['strength_value'] = value;
                              });
                            }
                          },
                          onChangeEnd: (val) async {
                            setState(() {
                              beauty[currentIndex]['strength_value'] = val;
                            });
                            onSliderEnd();
                          },
                          onChangeStart: (val) {},
                        ),
                      ),
                      margin: EdgeInsets.only(left: 20.w, right: 20.w),
                    ),
                    Container(
                      child: Text(
                        "肤色保护",
                        style: TextStyle(
                          fontSize: 12,
                          color: rgba(153, 153, 153, 1),
                        ),
                      ),
                      margin: EdgeInsets.only(
                        top: 5.w,
                      ),
                    ),
                    SizedBox(
                      height: 20.w,
                    ),
                    Text(
                      "${beauty[currentIndex]['skin_protect_value'].toInt()}",
                      style: TextStyle(
                        fontSize: 12,
                        color: rgba(51, 51, 51, 1),
                      ),
                    ),
                    Container(
                      child: SliderTheme(
                        data: SliderTheme.of(context).copyWith(
                          //已拖动的颜色
                          activeTrackColor: rgba(240, 69, 41, 1),
                          //未拖动的颜色
                          inactiveTrackColor: Color(0x36D8D8D8),
                          //提示进度的气泡的背景色
                          valueIndicatorColor: Color(0xCCD8D8D8),
                          //提示进度的气泡文本的颜色
                          valueIndicatorTextStyle: TextStyle(
                            color: Colors.white,
                          ),
                          trackHeight: 4.w,
                          tickMarkShape: RoundSliderTickMarkShape(
                            //继承SliderTickMarkShape可自定义刻度形状
                            tickMarkRadius: 0, //刻度大小
                          ),
                          thumbShape: RoundSliderThumbShape(
                            //可继承SliderComponentShape自定义形状
                            disabledThumbRadius: 0, //禁用时滑块大小
                            enabledThumbRadius: 8.w, //滑块大小
                          ),
                          overlayShape: RoundSliderOverlayShape(
                            //可继承SliderComponentShape自定义形状
                            overlayRadius: 0, //滑块外圈大小
                          ),
                          //滑块中心的颜色
                          thumbColor: Colors.white,
                        ),
                        child: Slider(
                          value: beauty[currentIndex]['skin_protect_value'],
                          max: max,
                          divisions: divisions,
                          onChanged: (double value) {
                            if (mounted) {
                              setState(() {
                                beauty[currentIndex]['skin_protect_value'] =
                                    value;
                              });
                            }
                          },
                          onChangeEnd: (val) async {
                            setState(() {
                              beauty[currentIndex]['skin_protect_value'] = val;
                            });
                            onSliderEnd();
                          },
                          onChangeStart: (val) {},
                        ),
                      ),
                      margin: EdgeInsets.only(left: 20.w, right: 20.w),
                    ),
                    Container(
                      child: Text(
                        "色彩增强",
                        style: TextStyle(
                          fontSize: 12,
                          color: rgba(153, 153, 153, 1),
                        ),
                      ),
                      margin: EdgeInsets.only(
                        top: 5.w,
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
        );
    }
    return Container();
  }

  //重置
  reset() {
    beauty.forEach((element) {
      if (element['type'] == "slider") {
        element['value'] = 0.0;
      } else if (element['suffix'] == darkLightLevel) {
        element['value'] = 1;
      } else if (element['suffix'] == contrast) {
        element['value'] = 1;
      } else if (element['suffix'] == ColorEnhancement) {
        element['button_value'] = 1;
        element['strength_value'] = 50.0;
        element['skin_protect_value'] = 100.0;
      }
    });
    setState(() {});
    //美颜
    setBeautyEffectOptions(false);
    //色彩增强
    setColorEnhanceOptions(false);
    //设置暗光增强功能
    setLowlightEnhanceOptions(false);
  }

  //暗光增强
  setLowlightEnhanceOptions(bool enabled) {
    widget.engine.setLowlightEnhanceOptions(
      enabled,
      LowLightEnhanceOptions(),
    );
  }

  //色彩增强 默认关闭
  setColorEnhanceOptions(
    bool enabled, {
    double strengthLevel: 0.5,
    double skinProtectLevel: 1.0,
  }) {
    widget.engine.setColorEnhanceOptions(
      enabled,
      ColorEnhanceOptions(
        //色彩增强程度。取值范围为 [0.0,1.0]。0.0 表示不对视频进行色彩增强。取值越大，色彩增强的程度越大。默认值为 0.5
        strengthLevel: strengthLevel,
        //肤色保护程度。取值范围为 [0.0,1.0]。0.0 表示不对肤色进行保护。取值越大，肤色保护的程度越大。默认值为 1.0
        skinProtectLevel: skinProtectLevel,
      ),
    );
  }

  //默认关闭美颜
  setBeautyEffectOptions(
    bool enabled, {
    double lighteningLevel: 0,
    double rednessLevel: 0,
    double smoothnessLevel: 0,
    LighteningContrastLevel lighteningContrastLevel:
        LighteningContrastLevel.Normal,
  }) {
    widget.engine.setBeautyEffectOptions(
      enabled,
      BeautyOptions(
        lighteningLevel: lighteningLevel,
        rednessLevel: rednessLevel,
        smoothnessLevel: smoothnessLevel,
        lighteningContrastLevel: lighteningContrastLevel,
      ),
    );
  }
}
